﻿using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
    internal class ServerRunStateService : IServerRunStateService
    {
        private readonly IServerRunStateCollect _RunState;
        private readonly IServerCollect _Server;

        public ServerRunStateService(IServerRunStateCollect runState, IServerCollect server)
        {
            this._Server = server;
            this._RunState = runState;
        }
        public ServerRunState Get(long serverId)
        {
            return this._RunState.Get(serverId);
        }

        public RunState[] Query(IBasicPage paging, out long count)
        {
            ServerRunState[] list = this._RunState.Query(paging, out count);
            if (list.Length == 0)
            {
                return System.Array.Empty<RunState>();
            }
            ServerConfigDatum[] servers = this._Server.Gets(list.ConvertAll(a => a.ServerId));
            return list.ConvertMap<ServerRunState, RunState>((a, b) =>
            {
                ServerConfigDatum datum = servers.Find(c => c.Id == a.ServerId);
                b.ServerName = datum.ServerName;
                b.IsContainer = datum.IsContainer;
                b.ServiceState = datum.ServiceState;
                b.IsOnline = datum.IsOnline;
                return b;
            });
        }
    }
}
